package cn.player.media.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;

import org.json.JSONObject;

import cn.player.R;
import cntv.player.core.util.LogUtil;
import cntv.player.core.util.Utils;
import cntv.player.engine.Const;

public abstract class BaseFragment extends Fragment {

	protected Context mContext;
	protected View rootView;

	protected FinalHttp finalHttp;
	protected FinalDb finalDb;
	protected FinalBitmap finalBitmap;

	protected Activity mActivity;


	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);


		mContext = getActivity();

		if (mActivity == null) {
			mActivity = getActivity();
		}

//		config = Config.getInstance();
//		config.setContext(mContext);

		if (finalHttp == null) {
//			finalHttp = config.getFinalHttp();
			finalHttp = new FinalHttp();
		}
		if (finalDb == null) {
//			finalDb = config.getFinalDb();
			finalDb = FinalDb.create(mContext, Const.DB_NAME);
		}
		if (finalBitmap == null) {
//			finalBitmap = config.getFinalBitmap();
			finalBitmap = FinalBitmap.create(mContext);
			if (Utils.getCachePath(mContext) != null) {
				finalBitmap.configDiskCachePath(Utils.getCachePath(mContext));
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
//		rootView = inflater.inflate(R.layout.player_base_fragment_layout, container,false);
		rootView = inflater.inflate(this.setContentLayout(), container,false);

//		initParent();
		if(rootView != null){
			findViews(rootView);
		}
		return rootView;
	}

	protected abstract int setContentLayout();
	protected  void findViews(View rootView){};
	protected  void setListensers(){};

	protected void initDataSuccess(String url,JSONObject response){}
	protected void initDataSuccess(String url,String response){}
	protected void initDataFailed(String url,int errorNo, String strMsg){}


	/**
	 * Afinal访问网络获取数据，
	 * 子类调用此方法 回调initDataSuccess()和initDataFailed()两方法
	 * 	默认不做异常处理
	 * @param url
	 */
	protected void initJsonData(String url) {
		initJsonData(url,false);
	}
	/**
	 * Afinal访问网络获取数据，
	 * 子类调用此方法 回调initDataSuccess()和initDataFailed() 两方法
	 * @param url
	 * @param isExceptionHandle 是否异常处理
	 * 	如果异常处理 则调用protected void initDataSuccess(String url,String response){}
	 * 	否则 调用protected void initDataSuccess(String url,JSONObject response){}
	 */
	protected void initJsonData(final String url,final boolean isExceptionHandle) {
		if(url == null){
			LogUtil.i("zl","initJsonData url = null");
			return;
		}
		if(url.trim().length() == 0){
			initDataFailed(url,Const.ERROR_REQUEST_NULL,getString(R.string.request_url_null));
			return;
		}
		finalHttp.get(url, new AjaxCallBack<String>(){
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
			}

			@Override
			public void onSuccess(String result) {
				// TODO Auto-generated method stub
				super.onSuccess(result);
//				System.out.println("mContext = " + mContext);
//				System.out.println("mActivity = " + mActivity);
//				System.out.println("getActivity() = " + getActivity());
				if(getActivity() == null){
					return;
				}
				try {
					if(result == null || "".equals(result.trim())){
						initDataFailed(url,Const.ERROR_RESPONSE_NULL,getString(R.string.server_data_null));
					}else{
						initDataSuccess(url, new JSONObject(result));
					}
				} catch (Exception e) {
					e.printStackTrace();
					if(getActivity() == null){
						return;
					}
					if(isExceptionHandle){
						initDataSuccess(url, result); //当服务器数据非json串格式需特殊处理的时候，调用
					}else{
						initDataFailed(url,Const.ERROR_DATA_EXCEPTION,getString(R.string.server_data_exception));
					}
				}
			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
//				Toast.makeText(mContext, "msg = " + strMsg, Toast.LENGTH_SHORT).show();
				if(getActivity() == null){
					return;
				}
				initDataFailed(url,errorNo,strMsg);
			}

		});
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		setListensers();
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
	}

}
